

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.ops.functional &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api_python/mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>mindspore.ops.functional</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for mindspore.ops.functional</h1><div class="highlight"><pre>
<span></span><span class="c1"># This is the Python adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).</span>
<span class="c1">#</span>
<span class="c1"># Copyright 2021 Huawei Technologies Co., Ltd</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ============================================================================</span>

<span class="sd">&quot;&quot;&quot;The names of functional part are summarized here.&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">mindspore.common._register_for_tensor</span> <span class="kn">import</span> <span class="n">tensor_operator_registry</span>
<span class="kn">from</span> <span class="nn">mindspore.common</span> <span class="kn">import</span> <span class="n">ms_function</span>
<span class="kn">from</span> <span class="nn">mindspore.common</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="kn">from</span> <span class="nn">mindspore.common</span> <span class="kn">import</span> <span class="n">dtype</span> <span class="k">as</span> <span class="n">mstype</span>
<span class="kn">from</span> <span class="nn">mindspore._checkparam</span> <span class="kn">import</span> <span class="n">Validator</span> <span class="k">as</span> <span class="n">validator</span>
<span class="kn">from</span> <span class="nn">mindspore._checkparam</span> <span class="kn">import</span> <span class="n">Rel</span>
<span class="kn">from</span> <span class="nn">mindspore.nn.grad.cell_grad</span> <span class="kn">import</span> <span class="n">_JvpInner</span>
<span class="kn">from</span> <span class="nn">mindspore.nn.grad.cell_grad</span> <span class="kn">import</span> <span class="n">_VjpInner</span>
<span class="kn">from</span> <span class="nn">mindspore.ops</span> <span class="kn">import</span> <span class="n">_constants</span>
<span class="kn">from</span> <span class="nn">mindspore.ops.primitive</span> <span class="kn">import</span> <span class="n">constexpr</span>
<span class="kn">from</span> <span class="nn">.primitive</span> <span class="kn">import</span> <span class="n">Primitive</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">operations</span> <span class="k">as</span> <span class="n">P</span>
<span class="kn">from</span> <span class="nn">.operations</span> <span class="kn">import</span> <span class="n">_grad_ops</span>
<span class="kn">from</span> <span class="nn">.operations</span> <span class="kn">import</span> <span class="n">_csr_ops</span>
<span class="kn">from</span> <span class="nn">.composite</span> <span class="kn">import</span> <span class="n">_Grad</span><span class="p">,</span> <span class="n">Shard</span>
<span class="kn">from</span> <span class="nn">.._c_expression</span> <span class="kn">import</span> <span class="n">security</span>

<span class="n">typeof</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;typeof&#39;</span><span class="p">)</span>
<span class="n">hastype</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;hastype&#39;</span><span class="p">)</span>
<span class="n">cast</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Cast</span><span class="p">()</span>
<span class="n">dtype</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">DType</span><span class="p">()</span>
<span class="n">isconstant</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;is_constant&#39;</span><span class="p">)</span>
<span class="n">isconstant</span><span class="o">.</span><span class="n">set_const_prim</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>

<span class="n">issubclass_</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">IsSubClass</span><span class="p">()</span>
<span class="n">isinstance_</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">IsInstance</span><span class="p">()</span>
<span class="n">eye</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Eye</span><span class="p">()</span>
<span class="n">fill</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Fill</span><span class="p">()</span>
<span class="n">tile</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Tile</span><span class="p">()</span>
<span class="n">size</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Size</span><span class="p">()</span>
<span class="n">ones_like</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">OnesLike</span><span class="p">()</span>
<span class="n">shape</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Shape</span><span class="p">()</span>
<span class="n">dyn_shape</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">DynamicShape</span><span class="p">()</span>
<span class="n">rank</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Rank</span><span class="p">()</span>
<span class="n">reshape</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Reshape</span><span class="p">()</span>

<span class="n">merge</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Merge</span><span class="p">()</span>
<span class="n">geswitch</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">GeSwitch</span><span class="p">()</span>
<span class="n">addn</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">AddN</span><span class="p">()</span>
<span class="n">absolute</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Abs</span><span class="p">()</span>
<span class="n">tensor_add</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Add</span><span class="p">()</span>
<span class="n">add</span> <span class="o">=</span> <span class="n">tensor_add</span>
<span class="n">neg_tensor</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Neg</span><span class="p">()</span>
<span class="n">tensor_lt</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Less</span><span class="p">()</span>
<span class="n">less</span> <span class="o">=</span> <span class="n">tensor_lt</span>
<span class="n">tensor_le</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">LessEqual</span><span class="p">()</span>
<span class="n">le</span> <span class="o">=</span> <span class="n">tensor_le</span>
<span class="n">tensor_gt</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Greater</span><span class="p">()</span>
<span class="n">gt</span> <span class="o">=</span> <span class="n">tensor_gt</span>
<span class="n">tensor_ge</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">GreaterEqual</span><span class="p">()</span>
<span class="n">ge</span> <span class="o">=</span> <span class="n">tensor_ge</span>
<span class="n">tensor_sub</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Sub</span><span class="p">()</span>
<span class="n">sub</span> <span class="o">=</span> <span class="n">tensor_sub</span>
<span class="n">tensor_mul</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Mul</span><span class="p">()</span>
<span class="n">mul</span> <span class="o">=</span> <span class="n">tensor_mul</span>
<span class="n">tensor_div</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">RealDiv</span><span class="p">()</span>
<span class="n">div</span> <span class="o">=</span> <span class="n">tensor_div</span>
<span class="n">tensor_floordiv</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">FloorDiv</span><span class="p">()</span>
<span class="n">floordiv</span> <span class="o">=</span> <span class="n">tensor_floordiv</span>
<span class="n">tensor_pow</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Pow</span><span class="p">()</span>
<span class="n">pows</span> <span class="o">=</span> <span class="n">tensor_pow</span>
<span class="n">tensor_mod</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">FloorMod</span><span class="p">()</span>
<span class="n">floormod</span> <span class="o">=</span> <span class="n">tensor_mod</span>
<span class="n">tensor_exp</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Exp</span><span class="p">()</span>
<span class="n">exp</span> <span class="o">=</span> <span class="n">tensor_exp</span>
<span class="n">tensor_expm1</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Expm1</span><span class="p">()</span>
<span class="n">tensor_slice</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Slice</span><span class="p">()</span>
<span class="n">strided_slice</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">StridedSlice</span><span class="p">()</span>
<span class="n">same_type_shape</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">SameTypeShape</span><span class="p">()</span>
<span class="n">check_bprop</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">CheckBprop</span><span class="p">()</span>
<span class="n">equal</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Equal</span><span class="p">()</span>
<span class="n">not_equal</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">NotEqual</span><span class="p">()</span>
<span class="n">isfinite</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">IsFinite</span><span class="p">()</span>
<span class="n">isnan</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">IsNan</span><span class="p">()</span>
<span class="n">assign_sub</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">AssignSub</span><span class="p">()</span>
<span class="n">assign_add</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">AssignAdd</span><span class="p">()</span>
<span class="n">assign</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Assign</span><span class="p">()</span>
<span class="n">square</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Square</span><span class="p">()</span>
<span class="n">sqrt</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Sqrt</span><span class="p">()</span>
<span class="n">log</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Log</span><span class="p">()</span>
<span class="n">reduce_sum</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceSum</span><span class="p">()</span>
<span class="n">reduce_max</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceMax</span><span class="p">()</span>
<span class="n">reduce_min</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceMin</span><span class="p">()</span>
<span class="n">reduce_mean</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceMean</span><span class="p">()</span>
<span class="n">reduce_prod</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceProd</span><span class="p">()</span>
<span class="n">tensor_slice</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Slice</span><span class="p">()</span>
<span class="n">maximum</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Maximum</span><span class="p">()</span>
<span class="n">minimum</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Minimum</span><span class="p">()</span>
<span class="n">floor</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Floor</span><span class="p">()</span>
<span class="n">logical_not</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">LogicalNot</span><span class="p">()</span>
<span class="n">logical_or</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">LogicalOr</span><span class="p">()</span>
<span class="n">logical_and</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">LogicalAnd</span><span class="p">()</span>
<span class="n">sin</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Sin</span><span class="p">()</span>
<span class="n">cos</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Cos</span><span class="p">()</span>
<span class="n">tan</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Tan</span><span class="p">()</span>
<span class="n">asin</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Asin</span><span class="p">()</span>
<span class="n">acos</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ACos</span><span class="p">()</span>
<span class="n">atan</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Atan</span><span class="p">()</span>
<span class="n">sinh</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Sinh</span><span class="p">()</span>
<span class="n">cosh</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Cosh</span><span class="p">()</span>
<span class="n">tanh</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Tanh</span><span class="p">()</span>
<span class="n">asinh</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Asinh</span><span class="p">()</span>
<span class="n">acosh</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Acosh</span><span class="p">()</span>
<span class="n">atanh</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Atanh</span><span class="p">()</span>
<span class="n">atan2</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Atan2</span><span class="p">()</span>
<span class="n">bitwise_and</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">BitwiseAnd</span><span class="p">()</span>
<span class="n">bitwise_or</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">BitwiseOr</span><span class="p">()</span>
<span class="n">bitwise_xor</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">BitwiseXor</span><span class="p">()</span>
<span class="n">invert</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Invert</span><span class="p">()</span>
<span class="n">erf</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Erf</span><span class="p">()</span>
<span class="n">erfc</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Erfc</span><span class="p">()</span>
<span class="n">sort</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Sort</span><span class="p">()</span>
<span class="n">tensor_range</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Range</span><span class="p">()</span>

<span class="n">scalar_to_array</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ScalarToArray</span><span class="p">()</span>
<span class="n">scalar_to_tensor</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ScalarToTensor</span><span class="p">()</span>
<span class="n">tuple_to_array</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">TupleToArray</span><span class="p">()</span>
<span class="n">scalar_cast</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ScalarCast</span><span class="p">()</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">security</span><span class="o">.</span><span class="n">enable_security</span><span class="p">():</span>
    <span class="n">print_</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Print</span><span class="p">()</span>
<span class="n">expand_dims</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ExpandDims</span><span class="p">()</span>
<span class="n">transpose</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Transpose</span><span class="p">()</span>
<span class="n">squeeze</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Squeeze</span><span class="p">()</span>
<span class="n">scatter_nd</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ScatterNd</span><span class="p">()</span>
<span class="n">gather</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Gather</span><span class="p">()</span>
<span class="n">gather_d</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">GatherD</span><span class="p">()</span>
<span class="n">gather_nd</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">GatherNd</span><span class="p">()</span>
<span class="n">scatter_update</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ScatterUpdate</span><span class="p">()</span>
<span class="n">tensor_scatter_update</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">TensorScatterUpdate</span><span class="p">()</span>
<span class="n">scatter_nd_update</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ScatterNdUpdate</span><span class="p">()</span>
<span class="n">stack</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Stack</span><span class="p">()</span>

<span class="n">csr_mul</span> <span class="o">=</span> <span class="n">_csr_ops</span><span class="o">.</span><span class="n">CSRMul</span><span class="p">()</span>
<span class="n">csr_mv</span> <span class="o">=</span> <span class="n">_csr_ops</span><span class="o">.</span><span class="n">CSRMV</span><span class="p">()</span>
<span class="n">csr_reduce_sum</span> <span class="o">=</span> <span class="n">_csr_ops</span><span class="o">.</span><span class="n">CSRReduceSum</span><span class="p">()</span>

<span class="n">_select</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Select</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">pack</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Call stack in this pack function.&quot;&quot;&quot;</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;WARNING: &#39;pack&#39; is deprecated from version 1.1 and will be removed in a future version, use &#39;stack&#39; instead&quot;</span>
          <span class="s2">&quot;.&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">stack</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>


<span class="n">partial</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Partial</span><span class="p">()</span>
<span class="c1"># depend: mount a node to another node</span>
<span class="n">depend</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Depend</span><span class="p">()</span>
<span class="n">identity</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">identity</span><span class="p">()</span>

<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_convert_grad_position_type</span><span class="p">(</span><span class="n">grad_position</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Check and convert the type and size of grad position index.&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">grad_position</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">gp</span> <span class="ow">in</span> <span class="n">grad_position</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">gp</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;For &#39;F.grad&#39;, the element in &#39;grad_position&#39; should be int, &quot;</span>
                                <span class="sa">f</span><span class="s2">&quot;but got </span><span class="si">{</span><span class="nb">type</span><span class="p">(</span><span class="n">gp</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">gp</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The element in grad_position must be &gt;= 0.&quot;</span><span class="p">)</span>
    <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">grad_position</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">grad_position</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;grad_position must be &gt;= 0.&quot;</span><span class="p">)</span>
        <span class="n">grad_position</span> <span class="o">=</span> <span class="p">(</span><span class="n">grad_position</span><span class="p">,)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;For &#39;F.grad&#39;, the &#39;grad_position&#39; should be int or tuple, &quot;</span>
                        <span class="sa">f</span><span class="s2">&quot;but got </span><span class="si">{</span><span class="nb">type</span><span class="p">(</span><span class="n">grad_position</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">grad_position</span>


<span class="n">grad_by_position</span> <span class="o">=</span> <span class="n">_Grad</span><span class="p">(</span><span class="n">get_by_list</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">sens_param</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">get_by_position</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">grad_by_position_with_sens</span> <span class="o">=</span> <span class="n">_Grad</span><span class="p">(</span><span class="n">get_by_list</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">sens_param</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">get_by_position</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

<div class="viewcode-block" id="grad"><a class="viewcode-back" href="../../../api_python/ops/mindspore.ops.grad.html#mindspore.ops.grad">[docs]</a><span class="k">def</span> <span class="nf">grad</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="n">grad_position</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">sens_param</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A wrapper function to generate the gradient function for the input function.</span>

<span class="sd">    Args:</span>
<span class="sd">        fn (Union(Cell, function)): Function to do GradOperation.</span>
<span class="sd">        grad_position (Union(int, tuple[int])): If int, get the gradient with respect to single input.</span>
<span class="sd">            If tuple, get the gradients with respect to selected inputs. &#39;grad_position&#39; begins with 0. Default: 0.</span>
<span class="sd">        sens_param (bool): Whether to append sensitivity (gradient with respect to output) as input.</span>
<span class="sd">            If sens_param is False, a &#39;ones_like(outputs)&#39; sensitivity will be attached automatically. Default: False.</span>

<span class="sd">    Returns:</span>
<span class="sd">        Function, returns the gradient function for the input function or cell.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; import mindspore.nn as nn</span>
<span class="sd">        &gt;&gt;&gt; import mindspore.context as context</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import Tensor</span>
<span class="sd">        &gt;&gt;&gt; from mindspore.ops.functional import grad</span>
<span class="sd">        &gt;&gt;&gt; context.set_context(mode=context.GRAPH_MODE)</span>
<span class="sd">        &gt;&gt;&gt; class Net(nn.Cell):</span>
<span class="sd">        ...     def construct(self, x, y, z):</span>
<span class="sd">        ...         return x*y*z</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))</span>
<span class="sd">        &gt;&gt;&gt; y = Tensor(np.array([[-2, 3], [-1, 2]]).astype(np.float32))</span>
<span class="sd">        &gt;&gt;&gt; z = Tensor(np.array([[0, 3], [5, -1]]).astype(np.float32))</span>
<span class="sd">        &gt;&gt;&gt; net = Net()</span>
<span class="sd">        &gt;&gt;&gt; output = grad(net, grad_position=(1, 2))(x, y, z)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        (Tensor(shape=[2, 2], dtype=Float32, value=</span>
<span class="sd">        [[ 0.00000000e+00,  6.00000000e+00],</span>
<span class="sd">         [ 1.50000000e+01, -4.00000000e+00]]), Tensor(shape=[2, 2], dtype=Float32, value=</span>
<span class="sd">        [[-2.00000000e+00,  6.00000000e+00],</span>
<span class="sd">         [-3.00000000e+00,  8.00000000e+00]]))</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">grad_position</span> <span class="o">=</span> <span class="n">_convert_grad_position_type</span><span class="p">(</span><span class="n">grad_position</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">sens_param</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">grad_by_position_with_sens</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">grad_position</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">grad_by_position</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">grad_position</span><span class="p">)</span></div>


<div class="viewcode-block" id="jvp"><a class="viewcode-back" href="../../../api_python/ops/mindspore.ops.jvp.html#mindspore.ops.jvp">[docs]</a><span class="k">def</span> <span class="nf">jvp</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="n">inputs</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute the jacobian-vector-product of the given network.</span>

<span class="sd">    Args:</span>
<span class="sd">        fn (Function or Cell): The function or net that takes Tensor inputs and returns a tensor or tuple of Tensors.</span>
<span class="sd">        inputs (Tensor or tuple or list): The inputs to `fn`.</span>
<span class="sd">        v (Tensor or tuple or list): The shape and type of v should be the same as inputs.</span>

<span class="sd">    Returns:</span>
<span class="sd">        Tuple, tuple of output and jvp.</span>

<span class="sd">        - **netout** (Tensors or Tuple of Tensors) - The output of &quot;fn(inputs)&quot;.</span>
<span class="sd">        - **jvp** (Tensors or Tuple of Tensors) - The result of the dot product.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If the input is not a tensor or tuple or list of tensors.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; from mindspore.ops import functional as F</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import Tensor</span>
<span class="sd">        &gt;&gt;&gt; class Net(nn.Cell):</span>
<span class="sd">        ...     def construct(self, x, y):</span>
<span class="sd">        ...         return x**3 + y</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))</span>
<span class="sd">        &gt;&gt;&gt; y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))</span>
<span class="sd">        &gt;&gt;&gt; v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))</span>
<span class="sd">        &gt;&gt;&gt; output = F.jvp(Net(), (x, y), (v, v))</span>
<span class="sd">        &gt;&gt;&gt; print(output[0])</span>
<span class="sd">        [[ 2. 10.]</span>
<span class="sd">         [30. 68.]]</span>
<span class="sd">        &gt;&gt;&gt; print(output[1])</span>
<span class="sd">        [[ 4. 13.]</span>
<span class="sd">         [28. 49.]]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">jvp_inner</span> <span class="o">=</span> <span class="n">_JvpInner</span><span class="p">()</span>

    <span class="nd">@ms_function</span>
    <span class="k">def</span> <span class="nf">_wrap_container</span><span class="p">(</span><span class="o">*</span><span class="n">arg</span><span class="p">):</span>
        <span class="n">args</span> <span class="o">=</span> <span class="n">arg</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="n">vectors</span> <span class="o">=</span> <span class="n">arg</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">jvp_inner</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="n">vectors</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">inputs</span><span class="p">,</span> <span class="p">(</span><span class="n">Tensor</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
        <span class="n">_raise_type_error</span><span class="p">()</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">inputs</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
        <span class="k">return</span> <span class="n">_wrap_container</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="o">*</span><span class="n">inputs</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">_wrap_container</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">inputs</span><span class="p">)</span></div>


<div class="viewcode-block" id="vjp"><a class="viewcode-back" href="../../../api_python/ops/mindspore.ops.vjp.html#mindspore.ops.vjp">[docs]</a><span class="k">def</span> <span class="nf">vjp</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="n">inputs</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute the vector-jacobian-product of the given network.</span>

<span class="sd">    Args:</span>
<span class="sd">        fn (Function or Cell): The function or net that takes Tensor inputs and returns a tensor or tuple of Tensors.</span>
<span class="sd">        inputs (Tensor or tuple or list): The inputs to `fn`.</span>
<span class="sd">        v (Tensor or tuple or list): The shape and type of v should be the same as outputs.</span>

<span class="sd">    Returns:</span>
<span class="sd">        Tuple, tuple of output and vjp.</span>

<span class="sd">        - **netout** (Tensors or Tuple of Tensors) - The output of &quot;fn(inputs)&quot;.</span>
<span class="sd">        - **vjp** (Tensors or Tuple of Tensors) - The result of the dot product.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If the input is not a tensor or tuple or list of tensors.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; from mindspore.ops import functional as F</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import Tensor</span>
<span class="sd">        &gt;&gt;&gt; class Net(nn.Cell):</span>
<span class="sd">        ...     def construct(self, x, y):</span>
<span class="sd">        ...         return x**3 + y</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))</span>
<span class="sd">        &gt;&gt;&gt; y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))</span>
<span class="sd">        &gt;&gt;&gt; v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))</span>
<span class="sd">        &gt;&gt;&gt; output = F.vjp(Net(), (x, y), v)</span>
<span class="sd">        &gt;&gt;&gt; print(output[0])</span>
<span class="sd">        [[ 2. 10.]</span>
<span class="sd">         [30. 68.]]</span>
<span class="sd">        &gt;&gt;&gt; print(output[1])</span>
<span class="sd">        (Tensor(shape=[2, 2], dtype=Float32, value=</span>
<span class="sd">        [[ 3.00000000e+00,  1.20000000e+01],</span>
<span class="sd">         [ 2.70000000e+01,  4.80000000e+01]]), Tensor(shape=[2, 2], dtype=Float32, value=</span>
<span class="sd">        [[ 1.00000000e+00,  1.00000000e+00],</span>
<span class="sd">         [ 1.00000000e+00,  1.00000000e+00]]))</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">vjp_inner</span> <span class="o">=</span> <span class="n">_VjpInner</span><span class="p">()</span>

    <span class="nd">@ms_function</span>
    <span class="k">def</span> <span class="nf">wrap_container</span><span class="p">(</span><span class="o">*</span><span class="n">arg</span><span class="p">):</span>
        <span class="n">args</span> <span class="o">=</span> <span class="n">arg</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">vectors</span> <span class="o">=</span> <span class="n">arg</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">vjp_inner</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">vectors</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">inputs</span><span class="p">,</span> <span class="p">(</span><span class="n">Tensor</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
        <span class="n">_raise_type_error</span><span class="p">()</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">inputs</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
        <span class="k">return</span> <span class="n">wrap_container</span><span class="p">(</span><span class="o">*</span><span class="n">inputs</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">wrap_container</span><span class="p">(</span><span class="n">inputs</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span></div>

<span class="n">shard_fn</span> <span class="o">=</span> <span class="n">Shard</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">shard</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="n">in_axes</span><span class="p">,</span> <span class="n">out_axes</span><span class="p">,</span> <span class="n">device</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">shard_fn</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="n">in_axes</span><span class="p">,</span> <span class="n">out_axes</span><span class="p">,</span> <span class="n">device</span><span class="p">,</span> <span class="n">level</span><span class="p">)</span>

<div class="viewcode-block" id="narrow"><a class="viewcode-back" href="../../../api_python/ops/mindspore.ops.narrow.html#mindspore.ops.narrow">[docs]</a><span class="k">def</span> <span class="nf">narrow</span><span class="p">(</span><span class="n">inputs</span><span class="p">,</span> <span class="n">axis</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">length</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns a narrowed tensor from input tensor.</span>
<span class="sd">    The dimension axis is input from start to start + length.</span>

<span class="sd">    Args:</span>
<span class="sd">        inputs (Tensor): the tensor to narrow.</span>
<span class="sd">        axis (int): the axis along which to narrow.</span>
<span class="sd">        start (int): the starting dimension.</span>
<span class="sd">        length (int): the distance to the ending dimension.</span>

<span class="sd">    Returns:</span>
<span class="sd">        Tensor.</span>

<span class="sd">        - output (Tensors) - The narrowed tensor.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If the input is not a tensor or tuple or list of tensors.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; import mindspore</span>
<span class="sd">        &gt;&gt;&gt; from mindspore.ops import functional as F</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import Tensor</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]], mindspore.int32)</span>
<span class="sd">        &gt;&gt;&gt; output = F.narrow(x, 0, 0, 2)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [[ 1 2 3],</span>
<span class="sd">         [ 4 5 6]]</span>
<span class="sd">        &gt;&gt;&gt; output = F.narrow(x, 1, 1, 2)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [[ 2 3],</span>
<span class="sd">         [ 5 6],</span>
<span class="sd">         [ 8 9]]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">validator</span><span class="o">.</span><span class="n">check_axis_in_range</span><span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="n">inputs</span><span class="o">.</span><span class="n">ndim</span><span class="p">)</span>
    <span class="n">validator</span><span class="o">.</span><span class="n">check_int_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">inputs</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="n">axis</span><span class="p">],</span> <span class="n">Rel</span><span class="o">.</span><span class="n">INC_LEFT</span><span class="p">)</span>
    <span class="n">validator</span><span class="o">.</span><span class="n">check_int_range</span><span class="p">(</span><span class="n">length</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">inputs</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span> <span class="o">-</span> <span class="n">start</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">INC_BOTH</span><span class="p">)</span>

    <span class="n">begins</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">inputs</span><span class="o">.</span><span class="n">ndim</span>
    <span class="n">begins</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span> <span class="o">=</span> <span class="n">start</span>
    <span class="n">sizes</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">inputs</span><span class="o">.</span><span class="n">shape</span><span class="p">]</span>
    <span class="n">sizes</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span> <span class="o">=</span> <span class="n">length</span>
    <span class="k">return</span> <span class="n">P</span><span class="o">.</span><span class="n">Slice</span><span class="p">()(</span><span class="n">inputs</span><span class="p">,</span> <span class="n">begins</span><span class="p">,</span> <span class="n">sizes</span><span class="p">)</span></div>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_raise_type_error</span><span class="p">():</span>
    <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;The inputs type should be a Tensor, tuple or list of Tensor.&quot;</span><span class="p">)</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_check_select_type_match</span><span class="p">(</span><span class="n">scalar</span><span class="p">,</span> <span class="n">tensor_type</span><span class="p">,</span> <span class="n">scalar_name</span><span class="p">,</span> <span class="n">tensor_name</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scalar</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="ow">and</span> <span class="n">tensor_type</span> <span class="o">!=</span> <span class="n">mstype</span><span class="o">.</span><span class="n">int32</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;For functional operator[select], the input[</span><span class="si">{</span><span class="n">scalar_name</span><span class="si">}</span><span class="s2">] is int, &quot;</span>
                        <span class="sa">f</span><span class="s2">&quot;then the input[</span><span class="si">{</span><span class="n">tensor_name</span><span class="si">}</span><span class="s2">] must be a Tensor of int32.&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scalar</span><span class="p">,</span> <span class="nb">float</span><span class="p">)</span> <span class="ow">and</span> <span class="n">tensor_type</span> <span class="o">!=</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;For functional operator[select], the input[</span><span class="si">{</span><span class="n">scalar_name</span><span class="si">}</span><span class="s2">] is float, &quot;</span>
                        <span class="sa">f</span><span class="s2">&quot;then the input[</span><span class="si">{</span><span class="n">tensor_name</span><span class="si">}</span><span class="s2">] must be a Tensor of float32.&quot;</span><span class="p">)</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_check_select_shape_match</span><span class="p">(</span><span class="n">input_shape</span><span class="p">,</span> <span class="n">cond_shape</span><span class="p">,</span> <span class="n">tensor_name</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">input_shape</span> <span class="o">!=</span> <span class="n">cond_shape</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;For functional operator[select], the cond shape must be same as </span><span class="si">{</span><span class="n">tensor_name</span><span class="si">}</span><span class="s2"> shape.&quot;</span><span class="p">)</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_check_select_type</span><span class="p">(</span><span class="n">is_cond_tensor</span><span class="p">,</span> <span class="n">is_x_scalar</span><span class="p">,</span> <span class="n">is_y_scalar</span><span class="p">,</span> <span class="n">is_x_tensor</span><span class="p">,</span> <span class="n">is_y_tensor</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">is_cond_tensor</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;For functional operator[select], the input[cond] must be a Tensor.&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">is_x_scalar</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">is_y_tensor</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;For functional operator[select], the input[x] is int or float, &quot;</span>
                        <span class="sa">f</span><span class="s2">&quot;then the input[y] must be a Tensor.&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">is_y_scalar</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">is_x_tensor</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;For functional operator[select], the input[y] is int or float, &quot;</span>
                        <span class="sa">f</span><span class="s2">&quot;then the input[x] must be a Tensor.&quot;</span><span class="p">)</span>


<div class="viewcode-block" id="select"><a class="viewcode-back" href="../../../api_python/ops/mindspore.ops.select.html#mindspore.ops.select">[docs]</a><span class="k">def</span> <span class="nf">select</span><span class="p">(</span><span class="n">cond</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns the selected elements, either from input :math:`x` or input :math:`y`, depending on the condition `cond`.</span>

<span class="sd">    Given a tensor as input, this operation inserts a dimension of 1 at the dimension,</span>
<span class="sd">    it was invalid when both :math:`x` and :math:`y` are none.</span>
<span class="sd">    Keep in mind that the shape of the output tensor can vary depending</span>
<span class="sd">    on how many true values are in the input. Indexes are output in row-first</span>
<span class="sd">    order.</span>

<span class="sd">    The conditional tensor acts as an optional compensation (mask), which</span>
<span class="sd">    determines whether the corresponding element / row in the output must be</span>
<span class="sd">    selected from :math:`x` (if true) or :math:`y` (if false) based on the value of each</span>
<span class="sd">    element.</span>

<span class="sd">    It can be defined as:</span>

<span class="sd">    .. math::</span>
<span class="sd">        out_i = \begin{cases}</span>
<span class="sd">        x_i, &amp; \text{if } condition_i \\</span>
<span class="sd">        y_i, &amp; \text{otherwise}</span>
<span class="sd">        \end{cases}</span>

<span class="sd">    If condition is a vector, then :math:`x` and :math:`y` are higher-dimensional matrices, then it</span>
<span class="sd">    chooses to copy that row (external dimensions) from :math:`x` and :math:`y`. If condition has</span>
<span class="sd">    the same shape as :math:`x` and :math:`y`, you can choose to copy these elements from :math:`x`</span>
<span class="sd">    and :math:`y`.</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **cond** (Tensor[bool]) - The shape is :math:`(x_1, x_2, ..., x_N, ..., x_R)`.</span>
<span class="sd">          The condition tensor, decides which element is chosen.</span>
<span class="sd">        - **x** (Union[Tensor, int, float]) - The shape is :math:`(x_1, x_2, ..., x_N, ..., x_R)`.</span>
<span class="sd">          The first input tensor. If x is int or float, it will be cast to the type of int32 or float32, and broadcast</span>
<span class="sd">          to the same shape as y. One of x and y must be a Tensor.</span>
<span class="sd">        - **y** (Union[Tensor, int, float]) - The shape is :math:`(x_1, x_2, ..., x_N, ..., x_R)`.</span>
<span class="sd">          The second input tensor. If y is int or float, it will be cast to the type of int32 or float32, and broadcast</span>
<span class="sd">          to the same shape as x. One of x and y must be a Tensor.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        Tensor, has the same shape as `cond`. The shape is :math:`(x_1, x_2, ..., x_N, ..., x_R)`.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If `x` or `y` is not a Tensor, int or float.</span>
<span class="sd">        ValueError: The shapes of inputs not equal.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; # 1) Both inputs are Tensor</span>
<span class="sd">        &gt;&gt;&gt; import mindspore</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import Tensor, ops</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; cond = Tensor([True, False])</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor([2,3], mindspore.float32)</span>
<span class="sd">        &gt;&gt;&gt; y = Tensor([1,2], mindspore.float32)</span>
<span class="sd">        &gt;&gt;&gt; output = ops.select(cond, x, y)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [2. 2.]</span>
<span class="sd">        &gt;&gt;&gt; # 2) y is a float</span>
<span class="sd">        &gt;&gt;&gt; cond = Tensor([True, False])</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor([2,3], mindspore.float32)</span>
<span class="sd">        &gt;&gt;&gt; y = 2.0</span>
<span class="sd">        &gt;&gt;&gt; output = ops.select(cond, x, y)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [2. 2.]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">is_x_scalar</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">))</span>
    <span class="n">is_y_scalar</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">))</span>
    <span class="n">is_x_tensor</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">Tensor</span><span class="p">)</span>
    <span class="n">is_y_tensor</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">Tensor</span><span class="p">)</span>
    <span class="n">is_cond_tensor</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cond</span><span class="p">,</span> <span class="n">Tensor</span><span class="p">)</span>
    <span class="n">_check_select_type</span><span class="p">(</span><span class="n">is_cond_tensor</span><span class="p">,</span> <span class="n">is_x_scalar</span><span class="p">,</span> <span class="n">is_y_scalar</span><span class="p">,</span> <span class="n">is_x_tensor</span><span class="p">,</span> <span class="n">is_y_tensor</span><span class="p">)</span>
    <span class="n">input_x</span> <span class="o">=</span> <span class="n">x</span>
    <span class="n">input_y</span> <span class="o">=</span> <span class="n">y</span>
    <span class="k">if</span> <span class="n">is_x_scalar</span><span class="p">:</span>
        <span class="n">_check_select_shape_match</span><span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">cond</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="s2">&quot;y&quot;</span><span class="p">)</span>
        <span class="n">_check_select_type_match</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="o">.</span><span class="n">dtype</span><span class="p">,</span> <span class="s2">&quot;x&quot;</span><span class="p">,</span> <span class="s2">&quot;y&quot;</span><span class="p">)</span>
        <span class="n">input_x</span> <span class="o">=</span> <span class="n">zeros_like</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="o">+</span> <span class="n">x</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
            <span class="n">input_x</span> <span class="o">=</span> <span class="n">cast</span><span class="p">(</span><span class="n">input_x</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">input_x</span> <span class="o">=</span> <span class="n">cast</span><span class="p">(</span><span class="n">input_x</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">is_y_scalar</span><span class="p">:</span>
        <span class="n">_check_select_shape_match</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">cond</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="s2">&quot;x&quot;</span><span class="p">)</span>
        <span class="n">_check_select_type_match</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="o">.</span><span class="n">dtype</span><span class="p">,</span> <span class="s2">&quot;y&quot;</span><span class="p">,</span> <span class="s2">&quot;x&quot;</span><span class="p">)</span>
        <span class="n">input_y</span> <span class="o">=</span> <span class="n">zeros_like</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">y</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
            <span class="n">input_y</span> <span class="o">=</span> <span class="n">cast</span><span class="p">(</span><span class="n">input_y</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">input_y</span> <span class="o">=</span> <span class="n">cast</span><span class="p">(</span><span class="n">input_y</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">_select</span><span class="p">(</span><span class="n">cond</span><span class="p">,</span> <span class="n">input_x</span><span class="p">,</span> <span class="n">input_y</span><span class="p">)</span></div>


<span class="n">tuple_setitem</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;tuple_setitem&#39;</span><span class="p">)</span>
<span class="n">tuple_getitem</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="n">_constants</span><span class="o">.</span><span class="n">kTupleGetItem</span><span class="p">)</span>
<span class="n">list_getitem</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;list_getitem&#39;</span><span class="p">)</span>
<span class="n">list_setitem</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;list_setitem&#39;</span><span class="p">)</span>
<span class="n">dict_getitem</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;dict_getitem&#39;</span><span class="p">)</span>
<span class="n">dict_setitem</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;dict_setitem&#39;</span><span class="p">)</span>
<span class="n">tuple_div</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;tuple_div&quot;</span><span class="p">)</span>
<span class="n">tuple_len</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;tuple_len&quot;</span><span class="p">)</span>
<span class="n">list_len</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;list_len&quot;</span><span class="p">)</span>
<span class="n">tuple_reversed</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;tuple_reversed&quot;</span><span class="p">)</span>
<span class="n">make_range</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;make_range&quot;</span><span class="p">)</span>
<span class="n">make_tuple</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;MakeTuple&#39;</span><span class="p">)</span>
<span class="n">make_dict</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;make_dict&#39;</span><span class="p">)</span>
<span class="n">make_list</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;make_list&#39;</span><span class="p">)</span>
<span class="n">make_slice</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;make_slice&#39;</span><span class="p">)</span>
<span class="n">tuple_equal</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;tuple_equal&quot;</span><span class="p">)</span>
<span class="n">list_equal</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;list_equal&quot;</span><span class="p">)</span>
<span class="n">make_ref</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;make_ref&quot;</span><span class="p">)</span>

<span class="n">scalar_add</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="n">_constants</span><span class="o">.</span><span class="n">kScalarAdd</span><span class="p">)</span>
<span class="n">scalar_mul</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="n">_constants</span><span class="o">.</span><span class="n">kScalarMul</span><span class="p">)</span>
<span class="n">scalar_sub</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="n">_constants</span><span class="o">.</span><span class="n">kScalarSub</span><span class="p">)</span>
<span class="n">scalar_div</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="n">_constants</span><span class="o">.</span><span class="n">kScalarDiv</span><span class="p">)</span>
<span class="n">scalar_floordiv</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="n">_constants</span><span class="o">.</span><span class="n">kScalarFloordiv</span><span class="p">)</span>
<span class="n">scalar_log</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;scalar_log&#39;</span><span class="p">)</span>
<span class="n">scalar_pow</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="n">_constants</span><span class="o">.</span><span class="n">kScalarPow</span><span class="p">)</span>
<span class="n">scalar_gt</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;scalar_gt&#39;</span><span class="p">)</span>
<span class="n">scalar_ge</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;scalar_ge&#39;</span><span class="p">)</span>
<span class="n">scalar_le</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;scalar_le&#39;</span><span class="p">)</span>
<span class="n">scalar_lt</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;scalar_lt&#39;</span><span class="p">)</span>
<span class="n">scalar_eq</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;scalar_eq&#39;</span><span class="p">)</span>
<span class="n">scalar_ne</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;scalar_ne&#39;</span><span class="p">)</span>
<span class="n">scalar_uadd</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="n">_constants</span><span class="o">.</span><span class="n">kScalarUadd</span><span class="p">)</span>
<span class="n">scalar_usub</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="n">_constants</span><span class="o">.</span><span class="n">kScalarUsub</span><span class="p">)</span>
<span class="n">scalar_mod</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="n">_constants</span><span class="o">.</span><span class="n">kScalarMod</span><span class="p">)</span>
<span class="n">string_eq</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;string_equal&#39;</span><span class="p">)</span>
<span class="n">string_concat</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;string_concat&#39;</span><span class="p">)</span>
<span class="n">bool_not</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;bool_not&quot;</span><span class="p">)</span>
<span class="n">bool_or</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;bool_or&quot;</span><span class="p">)</span>
<span class="n">bool_and</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;bool_and&quot;</span><span class="p">)</span>
<span class="n">bool_eq</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;bool_eq&quot;</span><span class="p">)</span>
<span class="n">logical_and</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">LogicalAnd</span><span class="p">()</span>
<span class="n">logical_or</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">LogicalOr</span><span class="p">()</span>
<span class="n">logical_not</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">LogicalNot</span><span class="p">()</span>
<span class="n">cumsum</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">CumSum</span><span class="p">()</span>
<span class="n">cumprod</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">CumProd</span><span class="p">()</span>
<span class="n">tensor_scatter_add</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">TensorScatterAdd</span><span class="p">()</span>
<span class="n">array_to_scalar</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;array_to_scalar&#39;</span><span class="p">)</span>
<span class="n">is_</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;is_&quot;</span><span class="p">)</span>
<span class="n">is_not</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;is_not&quot;</span><span class="p">)</span>
<span class="n">in_dict</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;in_dict&quot;</span><span class="p">)</span>
<span class="n">not_in_dict</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;not_in_dict&quot;</span><span class="p">)</span>
<span class="n">mixed_precision_cast</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;mixed_precision_cast&quot;</span><span class="p">)</span>
<span class="n">broadcast_gradient_args</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;BroadcastGradientArgs&#39;</span><span class="p">)</span>
<span class="n">array_reduce</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;array_reduce&#39;</span><span class="p">)</span>
<span class="n">zeros_like</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ZerosLike</span><span class="p">()</span>
<span class="n">distribute</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;distribute&#39;</span><span class="p">)</span>
<span class="n">embed</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;embed&#39;</span><span class="p">)</span>
<span class="n">ref_to_embed</span> <span class="o">=</span> <span class="n">_grad_ops</span><span class="o">.</span><span class="n">RefToEmbed</span><span class="p">()</span>
<span class="n">environ_create</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;EnvironCreate&#39;</span><span class="p">)</span>
<span class="n">environ_set</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;EnvironSet&#39;</span><span class="p">)</span>
<span class="n">environ_get</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;EnrironGet&#39;</span><span class="p">)</span>
<span class="n">environ_add</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;EnvironAdd&#39;</span><span class="p">)</span>
<span class="n">J</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;J&#39;</span><span class="p">)</span>
<span class="n">SliceGetItem</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;SliceGetItem&quot;</span><span class="p">)</span>
<span class="n">switch</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;Switch&#39;</span><span class="p">)</span>
<span class="n">switch_layer</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;switch_layer&#39;</span><span class="p">)</span>
<span class="c1"># for sum bprop</span>
<span class="n">reduced_shape</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;reduced_shape&quot;</span><span class="p">)</span>
<span class="c1"># shape_mul:input must be shape multiply elements in tuple(shape)</span>
<span class="n">shape_mul</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;shape_mul&quot;</span><span class="p">)</span>
<span class="c1"># a primitive to compare between tuple.</span>
<span class="n">stop_gradient</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s2">&quot;stop_gradient&quot;</span><span class="p">)</span>

<span class="n">make_row_tensor</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;MakeRowTensor&#39;</span><span class="p">)</span>
<span class="n">row_tensor_get_values</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;RowTensorGetValues&#39;</span><span class="p">)</span>
<span class="n">row_tensor_get_indices</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;RowTensorGetIndices&#39;</span><span class="p">)</span>
<span class="n">row_tensor_get_dense_shape</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;RowTensorGetDenseShape&#39;</span><span class="p">)</span>
<span class="n">row_tensor_add</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;RowTensorAdd&#39;</span><span class="p">)</span>

<span class="n">make_sparse_tensor</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;MakeSparseTensor&#39;</span><span class="p">)</span>
<span class="n">sparse_tensor_get_values</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;SparseTensorGetValues&#39;</span><span class="p">)</span>
<span class="n">sparse_tensor_get_indices</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;SparseTensorGetIndices&#39;</span><span class="p">)</span>
<span class="n">sparse_tensor_get_dense_shape</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;SparseTensorGetDenseShape&#39;</span><span class="p">)</span>

<span class="n">make_csr_tensor</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;MakeCSRTensor&#39;</span><span class="p">)</span>
<span class="n">csr_tensor_get_values</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;CSRTensorGetValues&#39;</span><span class="p">)</span>
<span class="n">csr_tensor_get_indices</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;CSRTensorGetIndices&#39;</span><span class="p">)</span>
<span class="n">csr_tensor_get_indptr</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;CSRTensorGetIndptr&#39;</span><span class="p">)</span>
<span class="n">csr_tensor_get_shape</span> <span class="o">=</span> <span class="n">Primitive</span><span class="p">(</span><span class="s1">&#39;CSRTensorGetDenseShape&#39;</span><span class="p">)</span>

<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;all&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceAll</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;any&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceAny</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;abs&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">Abs</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;mean&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceMean</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;reshape&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">Reshape</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;transpose&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">Transpose</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;broadcast_to&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">BroadcastTo</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;matmul&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">MatMul</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;argmax&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">Argmax</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;cumsum&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">CumSum</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;reduce_max&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceMax</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;reduce_min&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceMin</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;maximum&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">Maximum</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;minimum&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">Minimum</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;fill&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">Fill</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;tile&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">Tile</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;logical_not&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">LogicalNot</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;sum&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceSum</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;split&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">Split</span><span class="p">)</span>
<span class="c1"># ms cannot support Tensor(True) compare</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;__eq__&#39;</span><span class="p">,</span> <span class="n">equal</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;__ne__&#39;</span><span class="p">,</span> <span class="n">not_equal</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;__neg__&#39;</span><span class="p">,</span> <span class="n">neg_tensor</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;__lt__&#39;</span><span class="p">,</span> <span class="n">tensor_lt</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;__le__&#39;</span><span class="p">,</span> <span class="n">tensor_le</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;__gt__&#39;</span><span class="p">,</span> <span class="n">tensor_gt</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;__ge__&#39;</span><span class="p">,</span> <span class="n">tensor_ge</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;__logical_not__&#39;</span><span class="p">,</span> <span class="n">logical_not</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;shape&#39;</span><span class="p">,</span> <span class="n">shape</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;squeeze&#39;</span><span class="p">,</span> <span class="n">squeeze</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;expand_dims&#39;</span><span class="p">,</span> <span class="n">expand_dims</span><span class="p">)</span>
<span class="c1"># support GE backend for no compare operators</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;cast&#39;</span><span class="p">,</span> <span class="n">cast</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;shape_mul&#39;</span><span class="p">,</span> <span class="n">shape_mul</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;fill&#39;</span><span class="p">,</span> <span class="n">fill</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;concatenate&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">Concat</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;eye&#39;</span><span class="p">,</span> <span class="n">eye</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;reduce_sum&#39;</span><span class="p">,</span> <span class="n">reduce_sum</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;tensor_slice&#39;</span><span class="p">,</span> <span class="n">tensor_slice</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;select&#39;</span><span class="p">,</span> <span class="n">select</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;gather_d&#39;</span><span class="p">,</span> <span class="n">gather_d</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;gather_nd&#39;</span><span class="p">,</span> <span class="n">gather_nd</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;stack&#39;</span><span class="p">,</span> <span class="n">P</span><span class="o">.</span><span class="n">Stack</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;log&#39;</span><span class="p">,</span> <span class="n">log</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;floor&#39;</span><span class="p">,</span> <span class="n">floor</span><span class="p">)</span>
<span class="c1"># support sparse tensor operators</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;csr_mul&#39;</span><span class="p">,</span> <span class="n">csr_mul</span><span class="p">)</span>
<span class="n">tensor_operator_registry</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s1">&#39;narrow&#39;</span><span class="p">,</span> <span class="n">narrow</span><span class="p">)</span>
<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="n">name</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">()</span> <span class="k">if</span> <span class="n">name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="s2">&quot;_&quot;</span><span class="p">]</span>
<span class="n">__all__</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s1">&#39;Primitive&#39;</span><span class="p">)</span>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>